﻿var mrole = {
    //表格初始化
    gridInit: function () {
        $("#mrole-grid").datagrid({
            url: "/manage/role/grid",
            nowrap: false,
            rownumbers: true,
            border: false,
            fit: true,
            singleSelect: true,
            pageSize: 20,
            pagination: true,
            idField: "ID",
            toolbar: "#mrole-tools",
            columns:
            [[
                { field: "Name", title: "名称", width: 120, align: "center" },
                { field: "Remark", title: "描述", width: 280, align: "center" },
                { field: "Sequence", title: "排序", width: 50, align: "center" },
                {
                    field: "IsEnabled", title: "启用", width: 50, align: "center",
                    formatter: function (value, row, index) { return manage.iconYesOrNo(value); }
                },
                { field: "InsertTime", title: "添加时间", width: 140, align: "center" }
            ]]
        });
    },

    //添加
    insert: function () {
        mrole.mutual("添加", null);
    },
    //修改
    update: function () {
        var row = $("#mrole-grid").datagrid("getSelected");
        if (row) {
            mrole.mutual("修改", row.ID);
        }
        else { manage.slide(manage.nullRowMsg); }
    },
    //删除
    remove: function () {
        var row = $("#mrole-grid").datagrid("getSelected");
        if (row) {
            manage.confirm("确认删除?", function (del) {
                if (del) {
                    manage.ajax({
                        url: "/manage/role/remove",
                        type: "post", data: { id: row.ID },
                        success: function (result) {
                            manage.slide(result.message);
                            if (result.success) { mrole.reload(); }
                        }
                    });
                }
            });
        }
        else { manage.slide(manage.delNullRowMsg); }
    },
    //实体操作
    mutual: function (title, id) {
        var href = "/manage/role/page?" + $.param({ id: id });
        var d = manage.dialog({
            title: title, href: href, width: 500,
            handler: function () {
                manage.form({
                    dialog: d, formid: "#mrole-form",
                    url: "/manage/role/save",
                    success: function (result) { mrole.reload(); }
                });
            },
            onLoad: function () { mrole.formInit(); }
        });
    },
    //弹窗初始化
    formInit: function () {

    },

    //排序上移
    desc: function () {
        mrole.order("desc");
    },
    //排序下移
    asc: function () {
        mrole.order("asc");
    },
    //排序操作
    order: function (order) {
        var row = $("#mrole-grid").datagrid("getSelected");
        if (row) {
            manage.ajax({
                url: "/manage/role/setorder",
                type: "post", data: { id: row.ID, order: order },
                success: function (result) {
                    manage.slide(result.message);
                    if (result.success) {
                        $("#mrole-grid").datagrid("reload");
                    }
                }
            });
        }
        else { manage.slide(manage.nullRowMsg); }
    },

    //启用
    enable: function () {
        mrole.enableOrDisable(true);
    },
    //禁用
    disable: function () {
        mrole.enableOrDisable(false);
    },
    //启用禁用
    enableOrDisable: function (bool) {
        var row = $("#mrole-grid").datagrid("getSelected");
        if (row) {
            manage.ajax({
                url: "/manage/role/enableordisable",
                type: "post", data: { id: row.ID, bit: bool },
                success: function (result) {
                    manage.slide(result.message);
                    if (result.success) {
                        $("#mrole-grid").datagrid("reload");
                    }
                }
            });
        }
        else { manage.slide(manage.nullRowMsg); }
    },

    //刷新列表
    reload: function () {
        $("#mrole-grid").datagrid("unselectAll");
        $("#mrole-grid").datagrid("reload");
    },

    //权限
    purview: function () {
        var row = $("#mrole-grid").datagrid("getSelected");
        if (row) {
            var d = manage.dialog({
                title: "分配权限",
                width: 1020,
                height: $(window).height() - 20,
                href: "/manage/role/purview",
                toolbar: mrole.purviewTools(),
                handler: function () {
                    manage.ajax({
                        url: "/manage/role/setpurview",
                        type: "post",
                        data: { id: row.ID, data: mrole.purviewData() },
                        success: function (result) {
                            if (result.success) {
                                d.dialog("close");
                                manage.slide(result.message);
                            }
                            else { manage.msg(result.message); }
                        }
                    });
                },
                onLoad: function () { mrole.purviewInit(row.ID); }
            });
        }
        else { manage.slide(manage.nullRowMsg); }
    },
    purviewTools: function () {
        return [{
            iconCls: 'icon-edit-add', text: "全选", handler: function () {
                $("input[type='checkbox']", "#mrole-purviewbox").prop("checked", true);
            }
        }, {
            iconCls: 'icon-remove', text: "取消", handler: function () {
                $("input[type='checkbox']", "#mrole-purviewbox").prop("checked", false);
            }
        }, {
            iconCls: 'icon-undo', text: "折叠", handler: function () {
                $("#mrole-purviewgrid").treegrid("collapseAll");
            }
        }, {
            iconCls: 'icon-redo', text: "展开", handler: function () {
                $("#mrole-purviewgrid").treegrid("expandAll");
            }
        }];
    },
    purviewInit: function (id) {
        $("#mrole-purviewgrid").treegrid({
            title: "授权信息",
            url: "/manage/role/purviewgrid?" + $.param({ id: id }),
            rownumbers: true,
            fit: true,
            singleSelect: true,
            idField: "ID",
            treeField: "Name",
            parentField: "Pid",
            columns:
            [[
                { field: "Name", title: "名称", width: 200, align: "left" },
                {
                    field: "PurviewList", title: "权限", width: 610, align: "left",
                    formatter: function (value, row, index) {
                        if (value) {
                            var html = "<div id='mrole-purviewbox-" + row.ID + "' class='purviewbox'>";
                            for (var i = 0; i < value.length; i++) {
                                if (value[i].exist) {
                                    html += "<input mfid='" + value[i].ID + "' type='checkbox' checked='checked' />";
                                }
                                else {
                                    html += "<input mfid='" + value[i].ID + "' type='checkbox' />";
                                }
                                html += "<label>" + value[i].Name + "</label>";
                            }
                            return html + "</div>";
                        }
                    }
                },
                {
                    field: "Opt", title: "整行操作", width: 80, align: "center",
                    formatter: function (value, row, index) {
                        return manage.iconNoTextButton("icon-key-add", "全选", "mrole.purviewCheckRow(\"" + row.ID + "\")") +
                            manage.iconNoTextButton("icon-key-delete", "清除", "mrole.purviewUnCheckRow(\"" + row.ID + "\")");
                    }
                }
            ]]
        });
    },
    purviewData: function () {
        var cheks = $("input:checked", "#mrole-purviewbox");
        var data = [];
        for (var i = 0; i < cheks.length; i++) {
            data.push($(cheks[i]).attr("mfid"));
        }
        return JSON.stringify(data);
    },
    purviewCheckRow: function (id) {
        $("input[type='checkbox']", "#mrole-purviewbox-" + id).prop("checked", true);
    },
    purviewUnCheckRow: function (id) {
        $("input[type='checkbox']", "#mrole-purviewbox-" + id).prop("checked", false);
    }
};

$(function () { mrole.gridInit(); });